<template>
    <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible"
      :close-on-click-modal="false" draggable>
      <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
       <el-row :gutter="24">
    <el-col :span="12" class="mb20">
      <el-form-item label="任务标题" prop="title">
        <el-input v-model="form.title" placeholder="请输入任务标题"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="文件类型" prop="fileType">
        <el-input v-model="form.fileType" placeholder="请输入文件类型"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="工作路径" prop="execPpk">
        <el-input v-model="form.execPpk" placeholder="请输入工作路径"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="处理状态" prop="processStatus">
        <el-input v-model="form.processStatus" placeholder="请输入任务计算状态"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="提示信息" prop="message">
        <el-input v-model="form.message" placeholder="请输入提示信息"/>
      </el-form-item>
      </el-col>

		<el-col :span="12" class="mb20">
			<el-form-item label="省份" prop="province">
				<el-input v-model="form.province" placeholder="请输入省份"/>
			</el-form-item>
		</el-col>

		<el-col :span="12" class="mb20">
			<el-form-item label="城市" prop="city">
				<el-input v-model="form.city" placeholder="请输入城市"/>
			</el-form-item>
		</el-col>

		<el-col :span="12" class="mb20">
			<el-form-item label="区县" prop="district">
				<el-input v-model="form.district" placeholder="请输入区县"/>
			</el-form-item>
		</el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="开始时间" prop="startTime">
        <el-input v-model="form.startTime" placeholder="请输入任务开始时间"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="结束时间" prop="endTime">
        <el-input v-model="form.endTime" placeholder="请输入任务结束时间"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="来源客户端" prop="sourceClient">
        <el-input v-model="form.sourceClient" placeholder="请输入来源客户端"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="创建用户" prop="createUser">
        <el-input v-model="form.createUser" placeholder="请输入创建用户"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="CMCC ID" prop="cmccId">
        <el-input v-model="form.cmccId" placeholder="请输入CMCC ID"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="CMCC里程" prop="cmccMileage">
        <el-input v-model="form.cmccMileage" placeholder="请输入CMCC里程(公里)"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="CMCC日志" prop="cmccLog">
        <el-input v-model="form.cmccLog" placeholder="请输入CMCC日志"/>
      </el-form-item>
      </el-col>

			</el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">确 认</el-button>
        </span>
      </template>
    </el-dialog>
</template>

<script setup lang="ts" name="TaskDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj, validateExist } from '/@/api/task/taskInfo'
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典

// 提交表单数据
const form = reactive({
		id:'',
	  title: '',
	  fileType: '',
	  execPpk: '',
	  processStatus: '',
	  message: '',
	  startTime: '',
	  endTime: '',
	  sourceClient: '',
	  createUser: '',
	  cmccId: '',
	  cmccMileage: '',
	  cmccLog: '',
});

// 定义校验规则
const dataRules = ref({
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

  // 获取task信息
  if (id) {
    form.id = id
    getTaskData(id)
  }
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
    loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getTaskData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj({id: id}).then((res: any) => {
    Object.assign(form, res.data[0])
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>
